// pages/aMap/aMap.js
var service_common = require("../../service/common.js");
var service_version = require("../../service/version.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    Location: {},
    circles: [{
      latitude: "",
      longitude: "",
      color: "#4398ed42",
      fillColor: "#4398ed4f",
      radius: 0
    }],
    markers: [],
    offon:false
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    wx.hideShareMenu()
    var that = this
    var scale = 16
    wx.getSystemInfo({
      success(res) {
        console.log()
        if (getApp().globalData.userInfo.team_leader_level == 0) {//
          if (res.windowWidth < 375) {
            scale = 16
          } else if (res.windowWidth >= 375 && res.windowWidth <= 455) {
            scale = 16
          }
          that.data.circles[0].radius = 1000
          that.setData({
            circles: that.data.circles,
            scale: scale
          })

        } else if (getApp().globalData.userInfo.team_leader_level == 1) {
          if (res.windowWidth < 375) {
            scale = 14
          } else if (res.windowWidth >= 375 && res.windowWidth <= 455) {
            scale = 14
          }
          that.data.circles[0].radius = 3000
          that.setData({
            circles: that.data.circles,
            scale: scale
          })
        } else if (getApp().globalData.userInfo.team_leader_level == 2) {
          if (res.windowWidth < 375) {
            scale = 13
          } else if (res.windowWidth >= 375 && res.windowWidth <= 455) {
            scale = 14
          }
          that.data.circles[0].radius = 4000
          that.setData({
            circles: that.data.circles,
            scale: scale
          })
        } else if (getApp().globalData.userInfo.team_leader_level == 3) {
          if (res.windowWidth < 375) {
            scale = 13
          } else if (res.windowWidth >= 375 && res.windowWidth <= 455) {
            scale = 13
          }
          that.data.circles[0].radius = 6000
          that.setData({
            circles: that.data.circles,
            scale: scale
          })
        } else if (getApp().globalData.userInfo.team_leader_level >= 4) {
          console.log(res.windowWidth)
          if (res.windowWidth < 375) {
            scale = 13
          } else if (res.windowWidth >= 375 && res.windowWidth <= 455) {
            scale = 13
          }
          that.data.circles[0].radius = 8000
          that.setData({
            circles: that.data.circles,
            scale: scale
          })
        }
      }
    })
    // iphone5 17
    //
   
  },
  kefu_: function () {
    service_common.fetchData(
      'https://t.gxmp.henghi.com/uri/wxservice/sendDownloadApp',
      {
        openid: getApp().globalData.userInfo.openid,
        appid: getApp().globalData.app_id
      },
      (res) => {

      },
      "GET"
    )
  },
  // 前往下载app
  goAPP: function () {
    wx.showModal({
      title: '提示',
      content: '需要前去下载APP',
      confirmText: "前往",
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/aApp/aApp',
          })
        }
      }
    })

  },
  /**
   * 获取附近的红包
   */
  getRed: function (loc) {
    service_common.fetchData(
      service_common.getUrl("hb.redPacketDistanceList"),
      {
        openid: getApp().globalData.openid,
        lon: loc.longitude,
        lat: loc.latitude,
        distance: this.data.circles[0].radius/1000
      },
      (res) => {
        var hb_list = res.data.data.hb_list
        for (let i = 0; i < hb_list.length; i++) {
          hb_list[i].iconPath = '/image/hbao_sm2.png'
          hb_list[i].latitude = hb_list[i].lat
          hb_list[i].longitude = hb_list[i].lon
          hb_list[i].width = 40
          hb_list[i].height = 56
          hb_list[i].type = "red"
        }
        this.setData({
          markers: hb_list
        })
        
        // 获取附近的名片
        this.getNearP(loc.longitude + ',' + loc.latitude)
        //获取附近的门店
        this.getNearStores(loc.longitude, loc.latitude)
      },
      "GET"
    )
  },
  /**
   * 获取附近门店
   */
  getNearStores: function (lng, lat) {
    service_common.fetchData(
      service_common.getUrl("user.getNearStores"),
      {
        openid: getApp().globalData.openid,
        lng: lng,
        lat: lat
      },
      (res) => {
        if (res.data.code == 0) {
          let shopNear = res.data.data
          for (let i = 0; i < shopNear.length; i++) {
            shopNear[i].iconPath = '/image/shop_biao.png'
            shopNear[i].latitude = shopNear[i].lat
            shopNear[i].longitude = shopNear[i].lon
            shopNear[i].width = 40
            shopNear[i].height = 40
            shopNear[i].type = "shop"
          }
          this.data.markers = this.data.markers.concat(shopNear)
          this.setData({
            markers: this.data.markers
          })

        }
      },
      "GET"
    )
  },
  /**
 * 获取附近的人
 */
  getNearP: function (coordinates) {
    service_common.fetchData(
      service_common.getUrl("user.earUser"),
      {
        openid: getApp().globalData.openid,
        coordinates: coordinates,
        level: getApp().globalData.userInfo.team_leader_level+1
      },
      (res) => {
        if (res.data.code == 0) {
          let nearUser = res.data.data
          for (let i = 0; i < nearUser.length; i++) {
            nearUser[i].iconPath = '/image/circle.png'
            nearUser[i].latitude = nearUser[i].lat
            nearUser[i].longitude = nearUser[i].lon
            nearUser[i].width = 20
            nearUser[i].height = 20
            nearUser[i].type = "pe"
          }
          this.data.markers = this.data.markers.concat(nearUser)
          this.setData({
            markers: this.data.markers
          })
          console.log(this.data.markers)
        }
      },
      "GET"
    )
  },
  /**
   * 获取用户的定位
   */
  getLocation: function (cb) {
    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      success: (res) => {
        console.log(res)
        this.data.circles[0].latitude = res.latitude
        this.data.circles[0].longitude = res.longitude
        console.log(res)
        this.setData({
          Location: res,
          circles: this.data.circles
        })
        cb(res)
      }
    })
  },
  /**
   * 找到自己的位置
   */
  dwei: function () {
    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      success: (res) => {
        this.setData({
          Location: res
        })
      }
    })
  },
  /**
   * 点击了标点
   */
  markertap: function (e) {
    console.log(e)
    for (let i in this.data.markers) {
      if (e.markerId == this.data.markers[i].id) {
        if (this.data.markers[i].type == "red") {//红包
          wx.navigateTo({
            url: '/pages/aRed/aRed?id=' + e.markerId,
          })
        } else if (this.data.markers[i].type == "shop") {//名片
          wx.navigateTo({
            url: '/pages/aShop/aShop?id=' + this.data.markers[i].id,
          })

        } else {
          wx.navigateTo({
            url: '/pages/other/other?openid=' + this.data.markers[i].openid,
          })
        }
      }
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  idAuth:function(callback){
    wx.getSetting({
      success: (rel) => {
        if (rel.authSetting['scope.userInfo']) {
          // 已开启授权   
        } else {
          // 未开启授权
          this.setData({
            offon:true
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    if (wx.getStorageSync('isred')) {
      wx.setStorageSync("isred", false)
      this.getLocation((res) => {
        console.log(res)
        service_version.getIsAuditing( (ress)=>{
          if (ress.data.status != 0) {
            this.getRed(res)
          }
        })
        
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})